package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.entity.ServiceCartItem;
import com.yuyou.fn.tradingservice.entity.ServiceOrder;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.service.*;
import com.yuyou.fn.tradingservice.vo.ServiceOrderItemVo;
import org.springframework.stereotype.Service;

import com.yuyou.fn.tradingservice.mapper.ServiceOrderItemMapper;
import com.yuyou.fn.tradingservice.entity.ServiceOrderItem;
import com.yuyou.fn.tradingservice.service.IServiceOrderItemService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 *
 * ServiceOrderItem 表数据服务层接口实现类
 *
 */
@Service
public class ServiceOrderItemServiceImpl extends SuperServiceImpl<ServiceOrderItemMapper, ServiceOrderItem,Long> implements IServiceOrderItemService {

    @Resource
    private IServiceCartItemService cartItemService;

    @Resource
    private IServiceCartService cartService;

    @Resource
    private IServiceOrderService orderService;


    @Override
    public BigDecimal generateOrderItems(ServiceOrder order, List<Long> cartItemIds) {

        Double priceDouble = 0.00;
        for (Long cartItemId : cartItemIds) {
            ServiceCartItem cartItem = cartItemService.findById(cartItemId);
            priceDouble += cartItem.getPrice().doubleValue();
            ServiceOrderItem orderItem = new ServiceOrderItem();
            BeanUtils.copyProperties(orderItem, cartItem);
            orderItem.setStatus(0);
            BigDecimal quantity = BigDecimal.valueOf(cartItem.getQuantity());
            orderItem.setUnitPrice(cartItem.getPrice().divide(quantity));
            orderItem.setCreateTime(TimeUtils.now());
            orderItem.setUpdateTime(TimeUtils.now());
            orderItem.setOrderId(order.getOrderId());
            orderItem.setSn(order.getSn());
            //添加第一张图片
            if(cartItem.getImageUrl() != null && !"".equals(cartItem.getImageUrl())) {
                orderItem.setImageUrl(cartItem.getImageUrl());
            }
            insertSelective(orderItem);
        }
        //查询是否还有指定类型的购物条目
        Example<ServiceCartItem> example = ServiceCartItem.createExample();
        example.createCriteria().andEqualTo("type",order.getType());
        List<ServiceCartItem> cartItemList = cartItemService.findListByExample(example);
        if(cartItemList.size() == 0) {
            cartService.deleteById(cartItemService.findById(cartItemIds.get(0)).getCartId());
        }
        //删除购物车中的条目
        cartItemService.deleteBatchIds(cartItemIds);
        BigDecimal price = BigDecimal.valueOf(priceDouble);
        return price;

    }

    @Override
    public void generateSingleOrderItems(ServiceOrder order, ServiceProduct product, BigDecimal quantity) {
        ServiceOrderItem orderItem = new ServiceOrderItem();
        BeanUtils.copyProperties(orderItem, product);
        orderItem.setStatus(0);
        orderItem.setUnitPrice(product.getPrice());
        orderItem.setPrice(product.getPrice().multiply(quantity));
        orderItem.setCreateTime(TimeUtils.now());
        orderItem.setUpdateTime(TimeUtils.now());
        orderItem.setSn(order.getSn());
        orderItem.setOrderId(order.getOrderId());
        orderItem.setImageUrl(product.getImageUrl());
        insertSelective(orderItem);
    }

    @Override
    public List<ServiceOrderItem> findOrderItemListByOrderId(Long orderId) {
        Example<ServiceOrderItem> example = ServiceOrderItem.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderId" , orderId );
        return findListByExample(example);
    }

    @Override
    public ServiceOrderItemVo findOrderItemDetailById(Long orderItemId) {
        ServiceOrderItemVo vo = new ServiceOrderItemVo();
        ServiceOrderItem item = findById(orderItemId);
        vo.setOrder(orderService.findById(item.getOrderId()));
        BeanUtils.copyProperties(vo,item);
        return vo;
    }

    @Override
    public void changeOrderItemStatus(Long orderItemId,Integer status) {
        ServiceOrderItem item =findById(orderItemId);
        item.setStatus(status);
        updateSelectiveById(item);
        if(status==1){
            orderService.changOrderStatus(item.getOrderId(),1);
        }
    }

    @Override
    public void orderItemStatus(Long orderId) {
        List<ServiceOrderItem> orderItemList = this.findOrderItemListByOrderId(orderId);
        for (ServiceOrderItem orderItem : orderItemList) {
            orderItem.setStatus(1);
            this.updateSelectiveById(orderItem);
        }
    }

    @Override
    public void sendGoods(Long orderId) {
        List<ServiceOrderItem> orderItemList = this.findOrderItemListByOrderId(orderId);
        for (ServiceOrderItem orderItem : orderItemList) {
            orderItem.setStatus(2);
            this.updateSelectiveById(orderItem);
        }
    }

    @Override
    public void tradeSuccess(Long orderId) {
        List<ServiceOrderItem> orderItemList = this.findOrderItemListByOrderId(orderId);
        for (ServiceOrderItem orderItem : orderItemList) {
            orderItem.setStatus(3);
            this.updateSelectiveById(orderItem);
        }
    }

    @Override
    public List<Long> findRoomIds(Long busTeamId) {
        Example<ServiceOrderItem> example = ServiceOrderItem.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("busTeamId",busTeamId);
        //交易成功
        criteria.andEqualTo("status",3);
        List<ServiceOrderItem> serviceOrderItemList= findListByExample(example);
        Set<Long> roomIds = new HashSet<>();
        for (ServiceOrderItem item : serviceOrderItemList) {
            roomIds.add(item.getProductId());
        }
        return new ArrayList<>(roomIds);
    }
}